
package com.usersmodule.dao;
import com.usersmodule.bean.TbModule;
import com.usersmodule.bean.TbRoleModuleMiddle;

import java.sql.*;
import java.util.*;

import util.*;
public class TbModuleDAO {
    private Connection conn = null;
    private PreparedStatement pt = null;
    private ResultSet rs = null;
    private boolean flag = false;
    private String sql = "";
    private TbModule tbModule=null;
    private List<TbModule> tbModuleList=null;

    public boolean add(TbModule inTbModule){
        try{
        conn=ConnectDB.getSqlServerConnection();
        sql="insert into Tb_Module(ModuleName,ModuleUrl,ParentModuleId,IsEnable,ModuleMemo,ModuleSequence) values(?,?,?,?,?,?)";
        pt = conn.prepareStatement(sql);
        pt.setString(1,inTbModule.getModuleName());
        pt.setString(2,inTbModule.getModuleUrl());
        if(inTbModule.getParentModuleId()==0)
        	pt.setNull(3, Types.INTEGER);
        else
        	pt.setInt(3,inTbModule.getParentModuleId());
        pt.setInt(4,inTbModule.getIsEnable());
        pt.setString(5,inTbModule.getModuleMemo());
        pt.setInt(6,inTbModule.getModuleSequence());
        if (pt.executeUpdate() > 0) flag=true;
        }catch(Exception e){
            e.printStackTrace();
        }finally {
        	ConnectDB.closeSqlConnection();
        }
        return flag;
    }
    public boolean del(int inModuleId){
        return UtilDAO.del("Tb_Module", "ModuleId", inModuleId);
    }
    public boolean edit(TbModule inTbModule){
        try{
        conn=ConnectDB.getSqlServerConnection();
        sql="update Tb_Module set ModuleName=?,ModuleSequence=?,ModuleUrl=?,ParentModuleId=?,IsEnable=?,ModuleMemo=?,CreateTime=getdate() where ModuleId=?";
        pt = conn.prepareStatement(sql);
        pt.setString(1,inTbModule.getModuleName());
        pt.setInt(2, inTbModule.getModuleSequence());
        pt.setString(3,inTbModule.getModuleUrl());
        if(inTbModule.getParentModuleId()==0)
        	pt.setNull(4, Types.INTEGER);
        else
        	pt.setInt(4,inTbModule.getParentModuleId());
        pt.setInt(5,inTbModule.getIsEnable());
        pt.setString(6,inTbModule.getModuleMemo());
        pt.setInt(7,inTbModule.getModuleId());
        if (pt.executeUpdate() > 0) flag=true;
        }catch(Exception e){
            e.printStackTrace();
        }finally {
        	ConnectDB.closeSqlConnection();
        }
        return flag;
    }
    public TbModule getTbModuleByModuleId(int inModuleId) {
        tbModule=null;
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_Module where ModuleId=? ";
            pt = conn.prepareStatement(sql);
            pt.setInt(1,inModuleId);
            rs = pt.executeQuery();
            while (rs.next()) {
                if (tbModule == null)tbModule = getTbModule(rs);
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
        	ConnectDB.closeSqlConnection();
        }
        return tbModule;
    }
    public List<TbModule>  getTbModuleByParentModuleId(int inModuleId) {
    	 tbModuleList=new ArrayList<TbModule>();
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_Module where parentModuleId "+(inModuleId==0?"is null ":"="+inModuleId);
            pt = conn.prepareStatement(sql);
            rs = pt.executeQuery();
            while (rs.next()) {
                tbModuleList.add(getTbModule(rs));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
        	ConnectDB.closeSqlConnection();
        }
        return tbModuleList;
    }
    /**
     * 
     *@author qingyu zhang
     *@function:得到左边数据20110610新加
     * @param inModuleId
     * @return
     *2011-6-10
     *上午08:47:17
     *ProjectManagerT
     *com.modulemodule.dao
     *List<TbModule>
     */
    public List<TbModule>  getTbModuleByParentModuleIdOrderBy(int inModuleId) {
    	tbModuleList=new ArrayList<TbModule>();
    	try{
    		conn = ConnectDB.getSqlServerConnection();
    		sql = "select * from Tb_Module where parentModuleId "+(inModuleId==0?"is null ":"="+inModuleId)+"  order by moduleSequence ";
    		pt = conn.prepareStatement(sql);
    		rs = pt.executeQuery();
    		while (rs.next()) {
    			tbModuleList.add(getTbModule(rs));
    		}
    	}catch(Exception e){
    		e.printStackTrace();
    	}finally {
    		ConnectDB.closeSqlConnection();
    	}
    	return tbModuleList;
    }
    
    public List<TbModule> getTbModuleAll() {
    	List<TbModule> tbModuleLst=new ArrayList<TbModule>();
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_Module";
            pt = conn.prepareStatement(sql);
            rs = pt.executeQuery();
            while (rs.next()) {
            	tbModuleLst.add(getTbModule(rs));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
        	ConnectDB.closeSqlConnection();
        }
        return tbModuleLst;
    }
    public List<TbModule> getTbModuleAll(VskPage vp) {
        tbModuleList=new ArrayList<TbModule>();
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_Module";
            vp.setAllCountSql(sql);
            vp.getAllRecordCount(conn, vp.getAllCountSql());
            pt = conn.prepareStatement(vp.processPageSql( "Tb_Module", "ModuleId"));
            rs = pt.executeQuery();
            while (rs.next()) {
                tbModuleList.add(getTbModule(rs));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
        	ConnectDB.closeSqlConnection();
        }
        return tbModuleList;
    }
    public TbModule getTbModule(ResultSet rs) throws Exception{
        TbModule tbModule = new TbModule();
        tbModule.setModuleId(rs.getInt("ModuleId"));
        tbModule.setModuleName(rs.getString("ModuleName"));
        tbModule.setModuleSequence(rs.getInt("ModuleSequence"));
        tbModule.setModuleUrl(rs.getString("ModuleUrl"));
        tbModule.setParentModuleId(rs.getInt("ParentModuleId"));
        tbModule.setIsEnable(rs.getInt("IsEnable"));
        tbModule.setModuleMemo(rs.getString("ModuleMemo"));
        tbModule.setCreateTime(rs.getString("CreateTime"));
        return tbModule;
    }
    
   /**
    * 根据模块ID得到对应的模块列表
    * 
    */
    public List<TbModule> getModuleListByModuleIds(String moduleIds){
    	tbModuleList=new ArrayList<TbModule>();
    	try{
    		 conn = ConnectDB.getSqlServerConnection();
             sql = "select * from Tb_Module where ModuleId in("+moduleIds+")";
             rs = pt.executeQuery();
             while (rs.next()) {
                 tbModuleList.add(getTbModule(rs));
             }
    	}catch(Exception e){
    		e.printStackTrace();
    	}finally {
        	ConnectDB.closeSqlConnection();
        }
    	return tbModuleList;
    }
    /**
     * 
     *@author qingyu zhang
     *@function:根据角色模块中间表得到相应的模块列表
     * @param roleModuleMiddleList
     * @return
     *2011-5-27
     *上午10:07:49
     *ProjectManager
     *com.modulemodule.dao
     *List<TbModule>
     */
    public List<TbModule> getModuleListByModuleIds(List<TbRoleModuleMiddle> roleModuleMiddleList){
    	tbModuleList=new ArrayList<TbModule>();
    	if(roleModuleMiddleList!=null&&roleModuleMiddleList.size()>0){
    		String moduleIds = "";
    		for(TbRoleModuleMiddle roleModuleMiddle:roleModuleMiddleList){
    			if(moduleIds.equals("")){
    				moduleIds+=roleModuleMiddle.getModuleId();
    			}else{
    				moduleIds+=","+roleModuleMiddle.getModuleId();
    			}
    		}
    		if(!moduleIds.equals("")){
    			try{
        			conn = ConnectDB.getSqlServerConnection();
        			sql = "select * from Tb_Module where ModuleId in("+moduleIds+")";
        			rs = pt.executeQuery();
        			while (rs.next()) {
        				tbModuleList.add(getTbModule(rs));
        			}
        		}catch(Exception e){
        			e.printStackTrace();
        		}finally {
        			ConnectDB.closeSqlConnection();
        		}
    		}
    	}
    	return tbModuleList;
    }
    /**
     * 得到当前模块表中序列最大值
     *@author qingyu zhang
     *@function:
     * @return
     *2011-6-10
     *上午09:38:56
     *ProjectManagerT
     *com.modulemodule.dao
     *int
     */
    public int getMaxSequece(){
    	int maxNum = 0;
    	 try {
			conn = ConnectDB.getSqlServerConnection();
			String hql="select max(ModuleSequence) maxSeq from Tb_Module";
			pt = conn.prepareStatement(hql);
			rs = pt.executeQuery();
			if(rs.next()){
				maxNum= rs.getInt("maxSeq");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			ConnectDB.closeSqlConnection();
		}
		return maxNum;
    }
    public static void main(String[] args) {
	}
}
